<?php

    // +----------------------------------------------------------------------
    // | order模型
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\model\store;

    use app\pay\model\CouponCard;
    use app\pay\model\Pay;
    use app\pay\model\Payment;
	use app\model\ucenter\User;
    use think\model\concern\SoftDelete;
    use yicmf\tools\FlowSn;

    class Order extends Common
    {
        use SoftDelete;


        protected $status_lists = [
            -1 => '已取消',
            0 => '已关闭',
            1 => '未付款',
            2 => '已付款',
            3 => '已发货',
            5 => '已完结'
        ];


        /**
         * 所有日志
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function logs()
        {
            return $this->hasMany(OrderLog::class);
        }

        /**
         * 所有日志
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function evaluates()
        {
            return $this->hasMany(GoodEvaluate::class);
        }

        /**
         * FIXME:最新日志
         * @author  : 微尘 <yicmf@qq.com>
         */
        public function log()
        {
            return $this->hasOne(OrderLog::class);//->field('create_time,message')->order('create_time DESC');
        }
		public function address()
		{
			return $this->hasOne(OrderAddress::class);
		}
		public function express()
		{
			return $this->hasOne(OrderExpress::class);
		}
		/**
		 * 当前订单所使用的优惠券
		 */
		public function coupons()
		{
			return $this->belongsToMany(CouponCard::class,OrderCouponCard::class);
		}


        /**
         * 支付方式
         */
        public function payment()
        {
            return $this->belongsTo(Payment::class);
        }


        /**
         * 卖家
         */
        public function store()
        {
            return $this->belongsTo(Store::class);//->field('id,nickname,avatar,phone,introduction,follow,fan,income,store_info,store_address,store_phone,invite_id');
        }

        /**
         * 买家
         */
        public function user()
        {
            return $this->belongsTo(User::class);//->field('id,nickname,avatar,phone,introduction,follow,fan,income,store_info,store_address,store_phone,invite_id');
        }

        /**
         * 卖家
         */
        public function seller()
        {
            return $this->belongsTo(User::class,'seller_id');//->field('id,nickname,avatar,phone,introduction,follow,fan,income,store_info,store_address,store_phone,invite_id');
        }

        /**
         * 购买的商品
         */
        public function goods()
        {
            return $this->hasMany(OrderGood::class);
        }

        public function pay()
        {
            return $this->belongsTo(\app\model\pay\Pay::class,'sn','pay_sn');
        }
        /**
         * 写入修改
         * @param $data
         */
        public static function onBeforeInsert($data)
        {
            $data['sn'] = FlowSn::build(1);
            $data['ip'] = !empty($data['ip']) ? $data['ip'] : self::$request->ip();
            $data['status'] = 1;
        }
        protected $type = [
            'address_info'=>'array'
        ];
    }
